module("Block", package.seeall )

DEBUG = false

local BlockLimit = CreateConVar("nc_blocklimit", 90,  {FCVAR_REPLICATED} )
local BlockHealth = CreateConVar("nc_blockhealth", 6000,  {FCVAR_REPLICATED} )
local RepairTime = CreateConVar("nc_repairtime", 200,  {FCVAR_REPLICATED} ) //It will take this many seconds for an eng. completly repair a block
local AllowTeamDamage = CreateConVar("nc_block_team_damange", 1, {FCVAR_REPLICATED} )
local TrashHoldPerc = CreateConVar("nc_block_trashhold", 0.05, {FCVAR_REPLICATED} ) --The percentage of life of the block before it unfreezes in game
local RemoveProhibitedProps = CreateConVar("nc_remove_prohibited", "1", {FCVAR_REPLICATED} )

local WeldDistanceVar = CreateConVar("nc_weld_distance", 150, {FCVAR_REPLICATED} )
local UnweldMinTimeVar = CreateConVar("nc_unweld_min", 10, {FCVAR_REPLICATED} )
local UnweldMaxTimeVar = CreateConVar("nc_unweld_max", 30, {FCVAR_REPLICATED} )
local UnweldEnemyMulti = CreateConVar("nc_unweld_enemy", 2.0, {FCVAR_REPLICATED} )
local WeldMinTimeVar = CreateConVar("nc_weld_min", 10, {FCVAR_REPLICATED} )
local WeldMaxTimeVar = CreateConVar("nc_weld_max", 40, {FCVAR_REPLICATED} )

function GetBlocks()
	return ents.FindByClass( "sf_block_*")
end

function BlockCount( team )
	
	local Count = 0
	
	for _, ent in ipairs( GetBlocks() ) do
		if ent.Team && ent:Team() == team then
			Count = Count + 1
		end
	end
	
	return Count
	
end


function RemoveProhibited()
	return RemoveProhibitedProps:GetBool()
end

function WeldDistance()
	return WeldDistanceVar:GetInt()
end

function UnweldTimes()
	return UnweldMinTimeVar:GetInt(), UnweldMaxTimeVar:GetInt()
end

function WeldTimes()
	return WeldMinTimeVar:GetInt(), WeldMaxTimeVar:GetInt()
end

function MaxHealth()
	return BlockHealth:GetInt()
end

function TrashHold()
	return TrashHoldPerc:GetFloat() * MaxHealth()
end	

function MaxLimit()
	return BlockLimit:GetInt()
end

function AllowTeamDamange()
	return AllowTeamDamage:GetBool()
end

function InRange( ply, pos )
	return ply:EyePos():Distance( pos ) <= WeldDistance()
end

//Returns the amount of health a block should gain in one tick
function RepairLife()
	local MaxLife = MaxHealth()
	local Time = math.max( RepairTime:GetInt(), 0 )
	
	//The percentage that should be repaired per second
	return MaxLife / Time * FrameTime()

end

function ActiveGun( ply )
	local ActiveWeapon = ply:GetActiveWeapon()
	
	if ValidEntity( ActiveWeapon ) && ActiveWeapon:GetClass() == "weapon_physcannon" then
		return ActiveWeapon
	end
end 

function ValidBlock( ent )
	return IsValid( ent ) && ent.IsBlock
end

function AimOnBlock( ply )

	local Trace = ply:GetEyeTrace() 
	
	if ValidBlock( Trace.Entity ) then
		return Trace.Entity
	end

end

local function CalculateWeldTime( block, Min, Max )
	
	local Trashhold = TrashHold()
	local Life = block:Life()
	
	local Perc = (Life - Trashhold) / ( MaxHealth() - Trashhold )
	
	return Perc * (Max-Min) + Min	
	
end

function UnweldTime( block )
	return CalculateWeldTime( block, UnweldTimes() )
end

function WeldTime( block )
	return CalculateWeldTime( block, WeldTimes() )
end

function GetWeldTime( ply, block, Motion )
	if Motion == true then
		return WeldTime( block ) //It started as moving, so has to freeze, therefore weld
	end
	
	if ply:Team() == block:Team() then
		return UnweldTime( block ) //To unweld
	else
		return UnweldTime( block ) * UnweldEnemyMulti:GetFloat()
	end
end